<template>
  <div>
    <el-container>
      <el-header>
        <el-menu
          class="el-menu-demo"
          :default-active="'' + current"
          mode="horizontal"
        >
          <el-menu-item
            v-for="(item, indedx) in navs"
            :index="'' + indedx"
            @click="current = indedx"
            :class="{ active: indedx == current }"
            :key="item.id"
          >
            <i :class="'el-icon-' + item.menu_icon"></i>
            <span slot="title">{{ item.title }}</span>
          </el-menu-item>
          <el-button @click="btn" type="primary">退出登录</el-button>
        </el-menu>
      </el-header>
      <el-container>
        <el-aside width="200px">
          <el-menu
            router
            @open="menuOpend"
            :default-openeds="opened"
            :unique-opened="true"
            v-if="navs.length > 0"
          >
            <!-- 循环当前顶级菜单的子菜单 -->
            <template v-for="item in navs[current].children">
              <!-- 有三级菜单 -->
              <el-submenu
                v-if="item.children"
                :index="'' + item.id"
                :key="item.id"
              >
                <template slot="title">
                  <i :class="'el-icon-' + item.menu_icon"></i>
                  <span>{{ item.title }}</span>
                </template>
                <!-- 三级菜单 -->
                <el-menu-item
                  v-for="item in item.children"
                  :index="'' + item.id"
                  :key="item.id"
                  :route="{ name: navs[current].menu_url + item.menu_url }"
                >
                  {{ item.title }}
                </el-menu-item>
              </el-submenu>
              <!-- 没有三级菜单 -->
              <el-menu-item
                :index="{ name: item.menu_url }"
                :key="item.id"
                v-else
              >
                {{ item.title }}
              </el-menu-item>
            </template>
          </el-menu>
        </el-aside>
        <el-main>
          <!-- 显示每个页面的标题 -->
          <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item v-for="(item, index) in $route.meta.title" :key="index">{{
              item
            }}</el-breadcrumb-item>
          </el-breadcrumb>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<script>
import { navs } from "../api/api";
export default {
  components: {},
  data() {
    return {
      navs: [],
      opened: [sessionStorage.getItem("openId") || ""], // 默认展开的二级菜单的ID
      current: sessionStorage.getItem("current") || 0, // 当前每几个是选中的
    };
  },
  watch: {
    // 当 currnet 改变时函数就调用了 newValue改变之后的值
    current: function (newValue, oldValue) {
      sessionStorage.setItem("current", this.current);
    },
  },
  mounted() {
    navs().then((res) => {
      this.navs = res.data.rows;
    });
  },
  methods: {
    // 当二级菜单展开时触发
    menuOpend() {
      sessionStorage.setItem("openId");
    },
    // 退出登录
    btn() {
      localStorage.removeItem("token");
      this.$router.push({ path: "/login" });
    },
  },
  computed: {},
  watch: {},
};
</script>
<style scoped>
.el-header,
.el-footer {
  color: #333;
  text-align: center;
}
.el-aside {
  color: #333;
  text-align: center;
}

.el-main {
  color: #333;
  text-align: center;
}

body > .el-container {
  margin-bottom: 40px;
}
.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}
.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}
.el-menu-demo {
  width: 1000px;
  margin-left: 180px;
}
</style>